﻿using ConfExpert.Utility;
using DevExpress.XtraBars.Alerter;
using DevTeamIT;
using Extensions;
using System;
using System.Collections.Generic;

namespace DevTeamIT
{
    public class JobManager
    {
        public class Job
        {
            public string Caption { get; set; }
            public int ControlPeriodInSeconds { get; set; }
            public bool CanUserDisable { get; set; }
            public DateTime LastRun { get; set; }
            public static Job GetByCaption(string caption)
            {
                foreach (Job pa in PopupAlerts)
                    if (pa.Caption == caption)
                        return pa;

                return new Job();
            }

            public bool TimeToShow
            {
                get
                {
                    if (LastRun.SecondsPast() > ControlPeriodInSeconds)
                        return !(CanUserDisable && !Session.NotificationsEnabled);

                    return false;
                }
            }

            public void ShowPopup(string message)
            {
                JobManager.ShowPopup(Caption, message);
            }

            public delegate void AlertHandler(Job popupAlert);
            public event AlertHandler Alert;
            public event AlertHandler PopupClicked;

            internal void FireClicked()
            {
                if (PopupClicked != null)
                    PopupClicked(this);
            }

            internal void FireAlert()
            {
                if (Alert != null)
                    Alert(this);
            }
        }

        public static void ShowPopup(string Caption, string message)
        {
            AlertControl.Show(AppSession.MainForm, Caption, "\n" + message);
        }

        static AlertControl AlertControl;
        static System.Timers.Timer timer = null;
        static List<Job> PopupAlerts = new List<Job>();

        public static void Init()
        {
            AlertControl = new DevExpress.XtraBars.Alerter.AlertControl(AppSession.MainForm.Components);
            AlertControl.AutoFormDelay = 15000;
            AlertControl.AlertClick += AlertControl_AlertClick;
            timer = new System.Timers.Timer(60000);    //once a minute!
            //timer.SynchronizingObject = this;
            timer.Elapsed += timer_Elapsed;
            timer.Enabled = true;
        }

        public static void CheckAlerts()
        {
            foreach (Job pa in PopupAlerts)
                if (pa.TimeToShow)
                    pa.FireAlert();
        }

        static void AlertControl_AlertClick(object sender, AlertClickEventArgs e)
        {
            Job pa = Job.GetByCaption(e.Info.Caption);
            pa.FireClicked();
        }

        static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            CheckAlerts();
        }

        public static Job Add(string Caption, int ControlPeriodInSeconds, bool CanUserDisable)
        {
            Job alert = new Job();
            alert.Caption = Caption;
            alert.ControlPeriodInSeconds = ControlPeriodInSeconds;
            alert.CanUserDisable = CanUserDisable;
            alert.LastRun = new DateTime(2000, 1, 1);
            PopupAlerts.Add(alert);            
            return alert;
        }
    }
}
